package com.yssoft.controller.core;

import com.alibaba.fastjson.JSONObject;
import com.yssoft.entity.core.TDdDetail;
import com.yssoft.entity.core.TMaterial;
import com.yssoft.service.core.DdDetailService;
import com.yssoft.service.core.TMaterialService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author 陈翼东
 * @说明
 * @time 2018/4/28 下午 07:01
 */
@Controller
@RequestMapping("/material")
public class MaterialController {
    
    @Resource
    private TMaterialService materialService;
    @Resource
    private DdDetailService ddDetailService;
    
    /**
     * 跳转到物料管理页面
     *
     * @return
     */
    @RequestMapping("materialPage")
    public String materialPage() {
        return "core/materialList/materialList";
    }
    
    @RequestMapping("queryMaterialPage")
    @ResponseBody
    public JSONObject queryMaterialPage(@RequestParam(name = "page", defaultValue = "0") int page, @RequestParam(name = "limit", defaultValue = "10") int limit, String name, String manufacturer) {
        JSONObject jsonObject = materialService.queryMaterialPage(page, limit, name, manufacturer);
        return jsonObject;
    }
    
    /**
     * 跳转到添加人员信息页面
     *
     * @return
     */
    @RequestMapping("materialEditPage")
    public String personEditPage(String materialId, Model model) {
        if (materialId != null && materialId.trim().length() > 0) {
            //修改
            TMaterial material = materialService.queryMaterialById(Integer.parseInt(materialId));
            TDdDetail materialType = ddDetailService.queryDdDetailById(material.gettDId());
            model.addAttribute("material", material);
            if(materialType!=null){
                model.addAttribute("materialType", materialType);
            }
        }
        return "core/materialList/materialEdit";
    }
    
    /**
     * 跳转到查看人员信息页面
     *
     * @return
     */
    @RequestMapping("materialViewPage")
    public String materialViewPage(String materialId, Model model) {
        if (materialId != null && materialId.trim().length() > 0) {
            //修改
            TMaterial material = materialService.queryMaterialById(Integer.parseInt(materialId));
            TDdDetail materialType = ddDetailService.queryDdDetailById(material.gettDId());
            if(materialType!=null){
                model.addAttribute("materialType", materialType);
            }
           
            model.addAttribute("material", material);
        }
        return "core/materialList/materialView";
    }
    
    /**
     * 物料信息保存
     *
     * @param material
     * @return
     */
    @RequestMapping("materialSave")
    public String materialSave(TMaterial material,@RequestParam("file") MultipartFile file) {
        if (material.getId() != null) {
            materialService.editMaterial(material,file);
        } else {
            materialService.addMaterial(material,file);
        }
        return "redirect:materialPage";
    }
    
    /**
     * 单个删除物料信息
     *
     * @param id
     * @return
     */
    @RequestMapping("delMaterial")
    @ResponseBody
    public Integer delMaterial(String id) {
        return materialService.deleteMaterialById(id);
    }
    
    /**
     * 删除多个物料信息
     *
     * @param ids
     * @return
     */
    @RequestMapping("delMaterials")
    @ResponseBody
    public Integer delMaterials(String ids) {
        return materialService.deleteMaterialByIds(ids);
    }
    
    /**
     * 查询物料的tree
     * @return
     */
    @RequestMapping("queryMaterialsTree")
    public String queryMaterialsTree(Model model,String material){
        model.addAttribute("json",ddDetailService.queryMaterialDdDetailTree());
        model.addAttribute("material",material);
        return "core/ddDetail/materialsTree";
    }
    
}
